import java.applet.Applet;
import java.awt.Graphics;
import java.awt.TextArea;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;


public class LevelArray {
	
	
	public static void blanklvl(){
		for(int x = 0; x < 50; x ++){
			for(int y = 0; y < 50; y ++){
				Variables.Tiles[x][y] = Variables.TileTypes[152];
			}
		}
	}
	
	
	
	public static void loadLevel(String lvl){
		
		  try {
			  Variables.currentLvl = lvl;

			  URL url = new URL(Variables.directory, "files/Levels/"+lvl);
			  String line;
			  InputStream in = url.openStream();
			  BufferedReader bf = new BufferedReader(new InputStreamReader(in));
			  StringBuffer  strBuff = new StringBuffer();
			  int x = 0;
			  int y = 0;
			  while((line = bf.readLine()) != null){
				  //System.out.println(line);
				  int i = Integer.parseInt(line);
				  Variables.Tiles[y][x] = Variables.TileTypes[i];
				  Variables.TileId[y][x] = i;
				  x++;
				  if(x == 50){
					  x = 0;
					  y++;
				  }
			  }
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void loadDoor(){
		try {
			
			int i = 0;
			int doorX = 0;
			int doorY = 0;
			String doorLoc = "";
			
			String SubStringLvl = Variables.currentLvl.substring(0, Variables.currentLvl.length() - 4);
				
			System.out.println(SubStringLvl);
			
			URL url;
			url = new URL(Variables.directory, "files/Levels/"+SubStringLvl+"/"+Player.x/32+"-"+Player.y/32+".txt");
			String line;
			InputStream in = url.openStream();
			BufferedReader bf = new BufferedReader(new InputStreamReader(in));
			StringBuffer  strBuff = new StringBuffer();
			
			while((line = bf.readLine()) != null){
				System.out.println(line);
				if(i == 2){
					doorLoc = line;
				}
				if(i == 1){
					int arg = Integer.parseInt(line);
					doorY = arg;
					i++;
				}
				if(i == 0){
					int arg = Integer.parseInt(line);
					doorX = arg;
					i++;
				}
			}
			if(Variables.playerX/32 == doorX && Variables.playerY/32 == doorY){
				loadLevel(doorLoc);
			}else{
				System.out.println("No Door File :(");
				System.out.println(Variables.playerX+"  "+Variables.playerY);
			}
			System.out.println("DONE!");
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
	}
	public static void loadAI(){		
		System.out.println("Loading AI");					
	}
}